<?php

class Workset_Filter_TimestampToDate implements Zend_Filter_Interface {
	
	protected $_date = null;
	
	protected $_options = array();

	function __construct($options = null) {
    	if ($options !== null) {
			$this->setOptions($options);
		}
    }

	function filter($value) {
		$date = $this->_getDate();
		$date->setTimestamp((int)$value);
		$format = null;
		if (isset($this->_options['format'])) {
			$format = $this->_options['format'];
		}
		//overwrite Zend_Date MMMM format to stand-alone
		if (is_string($format) && preg_match("/^MMMM[^M]/", $format)) {
			$format = str_replace('MMMM', '', $format);
			$value = $date->toString($format);
			$month = date('n', $date->getTimestamp());
			$monthName = Zend_Locale_Data::getContent($date->getLocale(), 'month', array('gregorian', 'stand-alone', 'wide', $month));
			$value = $monthName.' '.$value;

			return $value;
		}
		return $date->toString($format);	
	}
	
	function setOptions($options) {
		if (count($options) == 1 && is_int(key($options))) {
			$options = array('format' => current($options));
		}
		$this->_options = $options;
	}
	
	function getOptions() {
        return $this->_options;
    }
	
	protected function _getDate() {
		if ($this->_date === null) {
			$this->_date = new Zend_Date();
			$locale = isset($this->_options['locale']) ? $this->_options['locale'] : Zend_Locale::findLocale(null);
			$this->_date->setLocale($locale);	
		}	
		return $this->_date;
	}
	
}

?>